package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.ScenicCommentVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.domain.ScenicComment;
import cn.wolfcode.luowowo.common.query.TravelCommentObject;
import cn.wolfcode.luowowo.common.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.webSite.annotation.RequireLogin;
import cn.wolfcode.luowowo.webSite.annotation.UserParam;
import cn.wolfcode.luowowo.webSite.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/mine")
public class MineController {


    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelCommentService travelCommentService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IScenicCommentMysqlService scenicCommentMysqlService;


    //跳转到主页面--->我的窝
    @RequestMapping("/homepage")
    @RequireLogin
    public String homepage(Model model, @UserParam UserInfo userInfo) {//用户的id共享到通用文件中

        //查询用户信息共享到界面做回显操作
        UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("userInfo",userInfo1);

        //查询所有游记信息
        List<Travel> list = travelService.list();
        model.addAttribute("list",list);

        //查询有多少篇游记共享到界面
        int count = travelService.selectCount();
        model.addAttribute("count",count);

        //我的关注(我是粉丝)
        List<Long> userId = travelService.selectAttentionMy(userInfo.getId());
        //我的关注(我是粉丝)
        List<UserInfo> userInfos = userInfoService.selectAttention(userId);
        model.addAttribute("userInfos",userInfos);
        //我的关注(我是粉丝)
        int userCount = userInfoService.selectAttentionMyCount(userInfo.getId());
        model.addAttribute("userCount",userCount);

        //我的粉丝
        int fansCount =  userInfoService.selectFans(userInfo.getId());
        model.addAttribute("fansCount",fansCount);

        return "personal/homepage";
    }


    //跳转游记内容页面
    @RequestMapping("/TravelContent")
    @RequireLogin
    public String TravelContent(Long id,Model model) {
        Travel detail = travelService.get(id);

        //共享所有的游记到跳转的游记页面
        model.addAttribute("detail",detail);

        //评论list
        TravelCommentObject qo = new TravelCommentObject();
        qo.setTravelId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = travelCommentService.query(qo);
        model.addAttribute("list", page.getContent());

        //toasts
        List<Destination> toasts = destinationService.getToasts(detail.getDest().getId());
        //中国 广东
        Destination dest = toasts.remove(toasts.size() - 1);
        //共享toasts
        model.addAttribute("toasts",toasts);

        //sds  关联攻略
        List<StrategyDetail> sds = strategyDetailService.getViewNumTop3(detail.getDest().getId());
        model.addAttribute("sds",sds);

        //ts  关联的游记
        List<Travel> ts = travelService.getViewNumTop3(detail.getDest().getId());
        model.addAttribute("ts",ts);

        return "personal/detail";
    }


    //我的游记页面
    @RequestMapping("/mytravelnotes")
    @RequireLogin
    public String mytravelnotes(Long id,Model model,@UserParam UserInfo userInfo) {
        //查询获取到游记的全部内容
        List<Travel> list = travelService.list();
        //把游记的全部内容共享到前端界面
        model.addAttribute("list",list);

        //查询用户信息共享到界面做回显操作
        UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("userInfo",userInfo1);

        //查询多少篇游记的信息
        int count = travelService.selectCount();
        model.addAttribute("count",count);

        //查询阅读数量的信息
        int viewnum = travelService.selectViewnum();
        model.addAttribute("viewnum",viewnum);

        //查询回复数量的信息
        int replynum = travelService.selectReplynum();
        model.addAttribute("replynum",replynum);

/*        //总访问量
        int totalViewNum=

        //今日访问量
        int todayViewNum-*/

        //访问量 + 1
        strategyStatisRedisService.viewnumIncrease(userInfo.getId(),1);
        model.addAttribute("visit",strategyStatisRedisService.getStrategyStatisVO(userInfo.getId()));

        //今日访问 + 1
        Long id1 = userInfo.getId();
        id1=1L;
        strategyStatisRedisService.viewnumIncrease(id1,1);
        model.addAttribute("visit1",strategyStatisRedisService.getStrategyStatisVO(id1));


        return "personal/mytravelnotes";
    }

    //我的游记点评 界面
    @RequestMapping("/review")
    @RequireLogin
    public String review(Long id,Model model,@UserParam  UserInfo userInfo){
        //查询信息共享到界面做回显操作
        List<ScenicCommentVO> scenicComments = scenicCommentMysqlService.selectRemark(userInfo.getId());
        for (ScenicCommentVO scenicComment : scenicComments) {
            ScenicPic pic = scenicComment.getDetailId().getPic().get(0);
            model.addAttribute("scenicPic",pic);
        }
        model.addAttribute("scenicComments",scenicComments);

        //查询用户信息共享到界面做回显操作
        UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("userInfo",userInfo1);

        //点评的数量
        int count = scenicCommentMysqlService.count(userInfo.getId());
        model.addAttribute("count",count);

        return "personal/review";
    }

    //我的游记收藏界面
    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelcollection(Long id,Model model,@UserParam UserInfo userInfo){
        //查询用户信息共享到界面做回显操作
        UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("userInfo",userInfo1);


        //获取攻略收藏的id
        List<Long> strategyId = strategyService.getStrategyId(userInfo.getId());
        //查询攻略收藏
        List<StrategyDetail> detail = strategyDetailService.getStrategyCollect(strategyId);
        model.addAttribute("detail",detail);


        //获取游记收藏的id
        List<Long> travelId = travelService.getTravelId(userInfo.getId());
        List<Travel> travel = travelService.getTravelCollect(travelId);
        model.addAttribute("travel",travel);

        return "personal/travelcollection";
    }

    //我的游记设置界面
    @RequestMapping("/setting")
    @RequireLogin
    public String setting(Long id,Model model,@UserParam UserInfo userInfo){
        //查询用户信息共享到界面做回显操作
        UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("userInfo",userInfo1);
        return "personal/setting";
    }


    //修改个人信息
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequireLogin
    public Object saveOrUpdate(UserInfo userInfo,Long id,Model model){
        userInfoService.saveOrUpdate(userInfo);
        return AjaxResult.SUCCESS;
    }

    //上传头像
    @Value("${file.path}")
    private String filePath;

    @RequestMapping("/file")
    @ResponseBody //插件要求这么写
    public Object upload(MultipartFile pic) {
        String filename = UploadUtil.upload(pic, filePath);
        return filename;
    }

    //保存头像
    @RequestMapping("/editIng")
    @ResponseBody
    public Object editIng(@UserParam  UserInfo userInfo,String headImgUrl,Long id){
        userInfoService.saveEditIng(id,headImgUrl);
        return AjaxResult.SUCCESS;
    }

    //修改密码
    @RequestMapping("/change")
    public String change(@UserParam UserInfo userInfo,Model model){
        return "personal/change";
    }


    //保存密码到数据库
    @RequestMapping("/amend")
    @ResponseBody
    public Object amend(@UserParam UserInfo userInfo,Model model,String password){
        userInfoService.update(userInfo.getId(),password);
        return AjaxResult.SUCCESS;
    }

}
